require 'extract/rules/xml_rule'
require 'singleton'
require 'enum'

module Extract
  
  class StumbleuponRules < Enum
    
    public
    
    class City < XMLRule
      def xpath
        "/html/body/div[@id='wrapper']/div[@id='wrapperContent']/div[@id='content']/div/div/p/a[contains(@href,'city')]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.locality = val
      end
    end
    
    class Country < XMLRule
      def xpath
        "/html/body/div[@id='wrapper']/div[@id='wrapperContent']/div[@id='content']/div/div/p/a[contains(@href,'country')]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.country = val
      end
    end
    
    class State < XMLRule
      def xpath
        "/html/body/div[@id='wrapper']/div[@id='wrapperContent']/div[@id='content']/div/div/p/a[contains(@href,'state')]"
      end
      
      protected
      
      def handle_val(val, dest_receiver)
        dest_receiver.state = val
      end
    end
    
    class Age < XMLRule
      def xpath
        "/html/body/div[@id='wrapper']/div[@id='wrapperContent']/div[@id='content']/div/div/p[contains(text(),'year old')]"
      end
      
      protected
      
      def preproces_val(val)
        m = val.match /(\d*) year old/
        val.replace m[1].nil? ? "" : m[1]
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.age = val.eql?("") ? nil : val.to_i
      end
    end
    
     class FullName < XMLRule
      def xpath
        "/html/body/div[@id='wrapper']/div[@id='wrapperContent']/div[@id='content']/div/div/p[@class = 'userInfo']"
      end
      
      protected
      
      def preproces_val(val)
        v = val.split /\n\t|\t/
        val.replace v[0].chomp ' is a'
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.full_name = val
      end
    end
    
    class FirstName < XMLRule
      def xpath
        "/html/body/div[@id='wrapper']/div[@id='wrapperContent']/div[@id='content']/div/div/p[@class = 'userInfo']"
      end
      
      protected
      
      def preproces_val(val)
        v = val.split /\n\t|\t/
        full_name = v[0].chomp ' is a'
        d = full_name.match /(\w+) (\w+)/
        val.replace d.nil? ? "" : d[1]
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.first_name = val.eql?("") ? nil : val
      end
    end
    
    class LastName < XMLRule
      def xpath
        "/html/body/div[@id='wrapper']/div[@id='wrapperContent']/div[@id='content']/div/div/p[@class = 'userInfo']"
      end
      
      protected
      
      def preproces_val(val)
        v = val.split /\n\t|\t/
        full_name = v[0].chomp ' is a'
        d = full_name.match /(\w+) (\w+)/
        val.replace d.nil? ? "" : d[2]
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.last_name = val.eql?("") ? nil : val
      end
    end
    
    class Gender < XMLRule
      def xpath
        "/html/body/div[@id='wrapper']/div[@id='wrapperContent']/div[@id='content']/div/div/p[@class = 'userInfo']"
      end
      
      protected
      
      def preproces_val(val)
        v = val.match /\W(man|woman)\W/
        if !v.nil? && v.size == 2 then
          val.replace "male" if v[1].eql?("man")
          val.replace "female" if v[1].eql?("woman")
        else
          val.replace ""
        end
        super val
      end
      
      def handle_val(val, dest_receiver)
        dest_receiver.gender = val.eql?("") ? nil : val
      end
    end
    
    protected
    
    def initialize
      super()
      @collections[:City] = City.instance
      @collections[:Country] = Country.instance
      @collections[:State] = State.instance
      @collections[:Age] = Age.instance
      @collections[:FullName] = FullName.instance
      @collections[:FirstName] = FirstName.instance
      @collections[:LastName] = LastName.instance
      @collections[:Gender] = Gender.instance
    end
    
  end
  
end